~ chicken-core (chicken-5) /manual/Accessing external objects
Trap1[[tags: manual]]23[[toc:]]45== Accessing externally defined data67For a list of the special forms that allow definition of Scheme procedures8that access native C/C++ code, consult the documentation on the9[[Module (chicken foreign)|(chicken foreign)]] module. The remainder10of this chapter merely explains a few special cases.1112== Returning large objects or chunks of memory to Scheme1314When you call a C function which needs to return quantities of data, several issues arise:15* the size of the nursery is limited, so {{C_alloc}} can cause stack overflow16* if you malloc in C, and intend to leave it there, and directly access parts of that data from Scheme, you will need C accessor functions to pinpoint the parts you need and return them as Scheme objects; you will also need a finalizer if you intend for this data to be garbage-collected17* building up lists or other complex Scheme structures from individual pairs, or putting non-immediate objects into vectors, is cumbersome in C1819So some would advise you to just return a pointer to Scheme, use memcpy or any other function(s) which you need to get the data into CHICKEN-managed memory and into the desired kind of data structure, then free the C data. For this example, we are trying to return an array of doubles into an {{f64vector}}; we can accomplish that by adding a specialized copy function to the C library being integrated:2021<enscript highlight=C>22void CopyResults(double* vector) {23 memcpy(vector, bezierBuffer, totalOutputPoints * sizeof(double));24}2526// The original C function which takes an array of doubles,27// does some sort of transmogrification,28// retains a new malloc'd array of the results29// and returns the count30int GenerateResults(double* vector, int count) {31 ...32}33</enscript>3435and the "egg" which calls the C functions can be implemented like this:3637<enscript highlight=scheme>38(module memcpy-demo (input->output)39 (import (chicken base) scheme (chicken foreign) srfi-4)4041 (define CopyResults (foreign-lambda void "CopyResults" f64vector))4243 (define GenerateResults (foreign-lambda integer "GenerateResults" f64vector integer))4445 (define (input->output input)46 (let* ([size (GenerateResults input (f64vector-length input))]47 [vect (make-f64vector size)])48 (printf "returned size ~a~%" size)49 (CopyResults vect)50 vect)))5152</enscript>5354The foreign-lambda takes care of the details in this case so that an f64vector allocated in the nursery can be treated as a plain old array of doubles in C (assuming your C compiler uses 64-bit values for double).5556Various eggs provide other examples, and some of them do it more efficiently too, but this method is relatively clean and compact.5758---59Previous: [[Interface to external functions and variables]]6061Next: [[Foreign type specifiers]]